import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login.vue'
import Main from '@/components/Main.vue'
import Welcome from '@/components/Welcome.vue'
import Users from '@/components/user/Users.vue'
import Rights from '@/components/power/Rights.vue'
import Roles from '@/components/power/Roles.vue'
import AddRoles from '@/components/power/AddRoles.vue'
import EditRoles from '@/components/power/EditRoles.vue'
import Categories from '@/components/goods/cate/Categories.vue'
import Params from '@/components/goods/params/Param.vue'
import Goods from '@/components/goods/good/Goods.vue'
import AddForm from '@/components/goods/good/AddForm.vue'
import Reports from '@/components/statistics/Reports.vue'
import Order from '@/components/order/Order.vue'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/main', redirect: '/welcome', component: Main, children: [
        { path: '/welcome', component: Welcome },
        {
          path: '/users', component: Users
        },
        { path: '/rights', component: Rights },
        {
          path: '/Roles', component: Roles, children: [
            { path: '/Roles/addRoles', component: AddRoles },
            { path: '/Roles/editRoles', component: EditRoles }
          ]
        },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goods/addForm', component: AddForm },
        { path: '/reports', component: Reports },
        { path: '/orders', component: Order },
      ]
    }
  ]
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  // form代表从哪个路径跳转而来
  // next() 直接放行  next("/login")强制跳转login
  if (to.path === '/login') {
    return next()//访问路径是login直接放行
  } else {//否则进行判断是否存在token
    //获取token
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) {
      return next("/login")
    } else {
      next()
    }
  }

})

export default router